<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-row :gutter="20" class="mb8">
        <el-form-item label="编号：" prop="caseCode">
          <el-input
            v-model="queryParams.caseCode"
            placeholder="请输入案件编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="文件表关联id" prop="fileId">
        <el-input
          v-model="queryParams.fileId"
          placeholder="请输入文件表关联id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> 
      </el-form-item>-->
        <el-form-item label="客户：" prop="customerId">
          <el-input
            v-model="queryParams.customerId"
            placeholder="请输入案件客户"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="律师：" prop="lawerId">
          <el-input
            v-model="queryParams.lawerId"
            placeholder="请输入案件律师"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-row :gutter="20" class="mb8">
        <el-form-item label="状态：" prop="caseStatus">
          <el-select
            v-model="queryParams.caseStatus"
            placeholder="请选择案件状态："
            clearable
            size="small"
          >
            <el-option
              v-for="dict in caseStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="类型：" prop="caseType">
          <el-select
            v-model="queryParams.caseType"
            placeholder="请选择案件类型："
            clearable
            size="small"
          >
            <el-option
              v-for="dict in caseTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:case:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- 
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:case:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:case:remove']"
          >删除</el-button
        > -->
      <!-- </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:case:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="caseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="案件编号" align="center" prop="caseCode" />
      <!-- <el-table-column label="文件表关联id" align="center" prop="fileId" /> -->
      <el-table-column label="客户" align="center" prop="customerId" />
      <el-table-column label="律师" align="center" prop="createBy" />
      <el-table-column label="相关资料" align="center" prop="fileId" >
        <template slot-scope="scope">
          <a :href="scope.row.fileId" type="button" style="color:blue;text-decoration:underline">下载</a>
        </template>
      </el-table-column>
      <el-table-column
        label="案件状态"
        align="center"
        prop="caseStatus"
        :formatter="caseStatusFormat"
      />
      <el-table-column
        label="案件类型"
        align="center"
        prop="caseType"
        :formatter="caseTypeFormat"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
          size="mini"
          type="button"
          icon="el-icon-success"
          @click="handeleSuccess(scope.row)"
          v-hasPermi="['business:case:edit']"
          >完成</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:case:query']"
            >查看</el-button
          > -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:case:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 完成案件 -->
    <el-dialog title="完成案件" :visible.sync="successOpen" width="500px" append-to-body>
       <el-form ref="caseForm" :model="caseForm" :rules="rules" label-width="80px">
        <el-form-item label="文件上传" prop="fileId">
          <fileUpload v-model="caseForm.fileId"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="caseForm.remark" placeholder="请输入备注"/>
        </el-form-item>
       </el-form>
       <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改case对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <!-- 步骤条 -->
        <div class="caseDetailStatus">
            <el-steps :active="activeStep" align-center>
                <el-step title="受理时间" description="未受理" @click.native="_step(0)" icon="el-icon-edit"></el-step>
                <el-step title="受理状态" description="受理状态" @click.native="_step(1)" icon="el-icon-info"></el-step>
                <el-step  title="结案" description="已完成" @click.native="_step(2)" icon="el-icon-s-check"></el-step>
            </el-steps>
        </div>
        
        <div style="box-sizing:border-box;padding:0 80px;" v-if="activeStep==1">
             <div class="block">
                    <el-timeline>
                     <el-timeline-item
                     v-for="(activity, index) in activities"
                     :key="index"
                     :icon="activity.icon"
                     :type="activity.type"
                     :color="activity.color"
                     :size="activity.size"
                     :timestamp="activity.caseDetailTime">
                     {{activity.caseDetailStatus}}
                    </el-timeline-item>
                 </el-timeline> 
                </div>
        </div>
        <div style="box-sizing:border-box;padding:0 80px;">
            <el-form v-if="activeStep==2" ref="form" :model="form" label-width="80px">
              <el-form-item label="案子状态">
                <template slot-scope="scope">
                    <el-radio v-model="scope.row.caseStatus" label="1" :disabled="diaDisable1">受理</el-radio>
                  <el-radio v-model="scope.row.caseStatus" label="2" :disabled="diaDisable2">结案</el-radio>
                  
                </template>
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" v-model="lawCaseDetailList.remark"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm" v-if="subDisabled">完成案件</el-button>
              </el-form-item>
            </el-form>
        </div>
       
      <!-- <el-form ref="form" :model="form" :rules="rules" label-width="200px"> -->

           <!-- <el-table
          :data="lawCaseDetailList"
          :row-class-name="rowLawCaseDetailIndex"
          @selection-change="handleLawCaseDetailSelectionChange"
          ref="lawCaseDetail"
          width="300px"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="案件状态" prop="caseDetailStatus">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.caseDetailStatus"
                placeholder="请输入案件状态：0.未受理1.已受理2.已完成"
              />
            </template>
          </el-table-column> 
          <el-table-column label="案件状态变更时间" prop="caseDetailTime">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.caseDetailTime"
                placeholder="请输入案件状态变更时间"
              />
            </template>
          </el-table-column>
          <el-table-column label="更新人" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入更新人" />
            </template>
          </el-table-column>
        </el-table>-->
        
        
      <!-- </el-form> -->
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关闭</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import {
  listCase,
  getCase,
  delCase,
  addCase,
  updateCase,
  exportCase,
} from "@/api/business/case";
import { getRole } from "@/api/business/book";
import FileUpload from '@/components/FileUpload';
export default {
  name: "Case",
  components: {FileUpload},
  data() {
    return {
      successOpen:false,
      // 遮罩层
      loading: true,
      diaDisable1:true,
      diaDisable2:true,
      subDisabled:true,
      //时间线正序
      reverse: true,
      details: [],
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedLawCaseDetail: [],
      activeStep: 1,
      caseDetailStatus: "0",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      activities: [
        {
          caseDetailStatus: "",
          caseDetailTime: ""
        }
      ],
      // case表格数据
      caseList: [],
      lawCaseDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 案件状态：0.未受理1.已受理2.已完成字典
      caseStatusOptions: [],
      // 案件类型字典
      caseTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseCode: null,
        fileId: null,
        caseDetailId: null,
        customerId:null,
        lawerId: null,
        caseStatus: null,
        caseType: null,
        createBy:null,
      },
      // 表单参数
      form: {},
      //
      caseForm:{
        remark:null,
        fileId:null,
        caseStatus:2,
        caseId:null,
      },
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("poppy_case_status").then((response) => {
      console.log("返回的类型字典")
      console.log(response)
      this.caseStatusOptions = response.data;
    });
    this.getDicts("poppy_question_type").then((response) => {
      this.caseTypeOptions = response.data;
    });
  },
  methods: {
    /**步骤条 */
    _step(i) {
      this.activeStep = i;
    },
    /** 查询case列表 */
    getList() {
      getRole().then(response=>{
        if(response.deptId == 108){
          this.queryParams.createBy = response.userName;
          
        }else if(response.deptId == 205){
          this.queryParams.customerId = response.userName;
          // this.loading = true;
          //       listCase(this.queryParams).then((response) => {
          //         this.caseList = response.rows;
          //         this.total = response.total;
          //         this.loading = false;
          //       });
        }
        this.loading = true;
                listCase(this.queryParams).then((response) => {
                  this.caseList = response.rows;
                  this.total = response.total;
                  this.loading = false;
                });
      })
      
    },
    
    // 案件状态：0.未受理1.已受理2.已完成字典翻译
    caseStatusFormat(row, column) {
      return this.selectDictLabel(this.caseStatusOptions, row.caseStatus);
    },
    // 案件类型字典翻译
    caseTypeFormat(row, column) {
      return this.selectDictLabel(this.caseTypeOptions, row.caseType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.successOpen = false,
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        caseId: null,
        caseCode: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        fileId: null,
        customerId: null,
        lawerId: null,
        caseStatus: null,
        caseType: null,
        remark: null,
        delFlag: null,
      };
      this.lawCaseDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.caseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改/查看按钮操作 */
    handleUpdate(row) {
        //使按钮不显示
        console.log(row)
      if(row.caseStatus==2){
          this.diaDisable1 = true
          this.diaDisable2 = true
          this.subDisabled = false
      }else if(row.caseStatus==1) {
          this.diaDisable1 = true
          this.diaDisable2 = false
          this.subDisabled = true
      }else{
          this.diaDisable1 = false
          this.diaDisable2 = false
          this.subDisabled = true
      }
      this.reset();
      const caseId = row.caseId || this.ids;
      getCase(caseId).then((response) => {
        this.form = response.data;
        this.lawCaseDetailList = response.data.lawCaseDetailList;
        this.activities = response.data.lawCaseDetailList
        console.log("数据是",this.activities)
        this.open = true;
        this.title = "查看案件进度";
      });
    },
    //完成案件状态
    handeleSuccess(row){
      this.caseForm.caseId = row.caseId;
      this.successOpen = true;

    },
    /** 提交按钮 */
    //弹出框里面添加状态，并可以修正状态
    submitForm() {
            updateCase(this.caseForm).then((response) => {
              this.msgSuccess("修改成功");
              this.successOpen = false;
              this.getList();
        })
        // if (valid) {
        //   this.form.lawCaseDetailList = this.lawCaseDetailList;
        //   if (this.form.caseId != null) {
        //     updateCase(this.form).then((response) => {
        //       this.msgSuccess("修改成功");
        //       this.open = false;
        //       this.getList();
        //     });
        //   } else {
        //     addCase(this.form).then((response) => {
        //       this.msgSuccess("新增成功");
        //       this.open = false;
        //       this.getList();
        //     });
        //   }
        // }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const caseIds = row.caseId || this.ids;
      this.$confirm(
        '是否确认删除case编号为"' + caseIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delCase(caseIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** ${subTable.functionName}序号 */
    rowLawCaseDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddLawCaseDetail() {
      let obj = {};
      obj.caseDetailStatus = "";
      obj.caseDetailTime = "";
      obj.remark = "";
      this.lawCaseDetailList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteLawCaseDetail() {
      if (this.checkedLawCaseDetail.length == 0) {
        this.$alert("请先选择要删除的${subTable.functionName}数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.lawCaseDetailList.splice(
          this.checkedLawCaseDetail[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleLawCaseDetailSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.lawCaseDetail.clearSelection();
        this.$refs.lawCaseDetail.toggleRowSelection(selection.pop());
      } else {
        this.checkedLawCaseDetail = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有case数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportCase(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style scoped>
.el-timeline {
  margin: 50px, auto;
  font-size: 14px;
  list-style: none;
}
</style>
